package org.anddev.andengine.extension.svg.node;
/**
 * @author Eric Depta
 * @since 01:14:00 - 28.02.2011
 */
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.extension.svg.SVGDoc;
import org.anddev.andengine.extension.svg.SVGStyle;
import org.anddev.andengine.extension.svg.util.parser.ParserUtil;
import org.anddev.andengine.extension.svg.util.physic.ExtendedPhysicsFactory;
import org.anddev.andengine.extension.svg.util.render.Render;
import org.anddev.andengine.extension.svg.util.transform.TransformDoc;
import org.anddev.andengine.util.SAXUtils;
import org.xml.sax.Attributes;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class RectNode extends DrawablePhysicNode{
	
	public RectNode(final SVGDoc pSVGDoc){
		super(pSVGDoc);
	}
	
	public void create(final Attributes pAttributes, final TransformDoc pTransform){
		super.create(pAttributes, pTransform);
	}
	
	@Override
	protected Shape createShape(final String pName, final Attributes pAttributes){
		final String source = SAXUtils.getAttribute(pAttributes, TAG_ATTRIBUTE_SPRITESRC, "");
		Shape shape = null;
		if(source != "" ){
			//-- Sprite
			shape = super.createSprite(
				pName, 
				pAttributes, 
				source,
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_X, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_Y, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_WIDTH, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_HEIGHT, 0),
				0
			);
			super.setHasSprite(true);
		}else if(super.isHasPhysic() || super.isDrawPrimitives()){
			//-- Boundings
			shape = new Rectangle(
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_X, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_Y, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_WIDTH, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_HEIGHT, 0)
			);
		}

		//-- Transform
		super.mTransformation.transformShape(shape);
		
		if(super.isDrawPrimitives()){
			final SVGStyle style = new SVGStyle(pAttributes.getValue(TAG_ATTRIBUTE_STYLE));
			//-- fill
			String colorstr = style.get(TAG_ATTRIBUTE_STYLE_FILLCOLOR); 
			if(!colorstr.equals("none")){
				final float[] color = ParserUtil.getColor(colorstr);
				shape.setColor(
					color[0], 
					color[1], 
					color[2], 
					style.getFloatValue(TAG_ATTRIBUTE_STYLE_FILLALPHA, 1)
				);
			}
			//-- outline
			colorstr = style.get(TAG_ATTRIBUTE_STYLE_LINECOLOR); 
			if(!colorstr.equals("none")){
				final Vector2[] verts = new Vector2[4];
				verts[0] = new Vector2(0,0);
				verts[1] = new Vector2(shape.getWidth(),0);
				verts[2] = new Vector2(shape.getWidth(),shape.getHeight());
				verts[3] = new Vector2(0,shape.getHeight());
				Render.lineRenderRel(
					verts, 
					shape, 
					ParserUtil.getColor(colorstr), 
					style.getFloatValue(TAG_ATTRIBUTE_STYLE_LINEALPHA, 1), 
					style.getFloatValuePx(TAG_ATTRIBUTE_STYLE_LINEWIDTH, 1),
					true
				);
			}
			
		}
		
		return shape;
	}
	
	@Override
	protected void createPhysics(final Shape pShape, final String pName, final Attributes pAttributes){
		if(super.isHasPhysic()){
			final Body body = ExtendedPhysicsFactory.createBoxBody(
				super.mSVGDoc.mPhysicsWorld, 
				pShape,
				BodyType.values()[SAXUtils.getIntAttribute(pAttributes, TAG_ATTRIBUTE_BODYTYPE, 0)], 
				super.createFixtureDef(pAttributes)
			);
			super.setBodyOptions(pAttributes, body);
			super.registerPhysicsConnector(pAttributes, pShape, body);
			super.mSVGDoc.addBody(pName, body);
		}else if(super.isHasSprite()){
			//-- Velocitys to shape
			super.createPhysicHandler(pAttributes, pShape);
		}
	}
}
